package com.yiwei.jmx.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.IncomeCertV3Bean;
import com.yiwei.jmx.api.bean.UploadImgBean;
import com.yiwei.jmx.api.model.IncomeCertConfModel;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.ImageUtil;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.SelectMenuDilaog;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;
import com.yiwei.jmx.view.uploadImage.UploadImgAdapter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class IncomeIdentityActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_address)
    TextView btnAddress;
    @BindView(R.id.btn_income)
    TextView btnIncome;
    @BindView(R.id.btn_example)
    TextView btnExample;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.btn_take_photo)
    ImageView btnTakePhoto;
    @BindView(R.id.btn_retake_photo)
    SuperButton btnRetakePhoto;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    private Unbinder unbiner;
    private SelectMenuTipsDilaog selectMenuTipsDilaog;
    private UploadImgAdapter uploadImgAdapter;
    private int selectPoi;
    private IncomeCertConfModel.DataBean totalData;
    private SelectMenuDilaog selectMenuDilaog;
    private IncomeCertV3Bean submitBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_identity);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        submitBean = new IncomeCertV3Bean();
        selectMenuDilaog = new SelectMenuDilaog(this);
        selectMenuDilaog.setOnChooseListener(new SelectMenuDilaog.OnChooseListener() {
            @Override
            public void onSuccess(int poi, String text, String menuType) {
                if (menuType.equals("send_type")) {
                    btnAddress.setText(text);
                    submitBean.send_type = text;
                } else if (menuType.equals("range")) {
                    btnIncome.setText(text);
                    submitBean.range = text;
                }
            }
        });

        txtTitle.setText("收入认证");
        selectMenuTipsDilaog = new SelectMenuTipsDilaog(this).setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                finish();
            }

            @Override
            public void onRight() {
                selectMenuTipsDilaog.dismiss();
            }
        }).showTwoSelectData("主人", R.mipmap.img_idcard_fail_dialog, "只有认证成功才能获取更高智信分您确定要离开吗？", "残忍离开", "我要留下");

        getIncomeCertConf();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    public void getUploadImage(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        UploadImgBean bean = new UploadImgBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.getUploadImage(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (selectPoi == 0) {
                        Glide.with(this).load(result.url).into(imgPhoto);
                    }
                    submitBean.image = result.getUrl();
                });
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImage(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {

            }
        }, 1);
    }

    private void getIncomeCertConf() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getIncomeCertConf().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    totalData = result.data;
                });
    }

    private void getIncomeIndex() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getIncomeCertIndex(submitBean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "提交成功");
                    finish();
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        selectMenuTipsDilaog.show();
    }

    @OnClick({R.id.btn_income, R.id.btn_back, R.id.btn_address, R.id.btn_example, R.id.btn_take_photo, R.id.btn_retake_photo, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                selectMenuTipsDilaog.show();
                break;
            case R.id.btn_address:
                if(totalData==null){
                    getIncomeCertConf();
                    return;
                }
                selectMenuDilaog.setData("send_type", totalData.send_type);
                selectMenuDilaog.show();
                break;
            case R.id.btn_income:
                if(totalData==null){
                    getIncomeCertConf();
                    return;
                }
                selectMenuDilaog.setData("range", totalData.range);
                selectMenuDilaog.show();
                break;
            case R.id.btn_example:
                Util.startActivity(this, IncomeExampleActivity.class);
                break;
            case R.id.btn_take_photo:
            case R.id.btn_retake_photo:
                selectPoi = 0;
                checkPermission(new CheckPermListener() {
                    @Override
                    public void superPermission() {
                        if (uploadImgAdapter == null) {
                            initUploadImgAdapter();
                        }
                        uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
                    }

                    @Override
                    public void onReject() {

                    }
                }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.btn_submit:
                if (submitBean.checkAll()) {
                    getIncomeIndex();
                }
                break;
        }
    }
}
